# -*- coding: utf-8 -*-
# time: 2025/4/2 14:50
# file: ch01.py
# author: hanson
import pdfplumber
from PyPDF2 import PdfReader
Load_path = r"F:\workspace\py_project\intellect\llm\pdf"
# 打开PDF文件
with open(Load_path+"\LLM_Survey_Chinese.pdf", "rb") as file:
    # 创建 PdfReader 对象
    pdf_reader = PdfReader(file)

    # 获取总页数
    total_pages = len(pdf_reader.pages)
    print(f"PDF 总页数: {total_pages}")

    # 逐页提取文本
    for page_num in range(total_pages):
        page = pdf_reader.pages[page_num]  # 直接通过索引获取页面‌:ml-citation{ref="1,5" data="citationList"}
        text = page.extract_text()  # 示例：尝试 GBK 编码‌:ml-citation{ref="8" data="citationList"}

       # text = page.extract_text()  # 提取文本内容‌:ml-citation{ref="1,3" data="citationList"}
        print(f"第 {page_num + 1} 页内容:\\n{text}")

# 我们发现上面PyPDF2 是提取的文本内容是乱码，需要使用pdfplumber
with pdfplumber.open(Load_path+"\LLM_Survey_Chinese.pdf") as pdf:
    for page in pdf.pages:  # 逐页处理
        text = page.extract_text()  # 提取当前页全部文本 ‌:ml-citation{ref="2,6" data="citationList"}
        print(text)